﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Inventory
{
    /// <summary>
    /// Interaction logic for dlgAdjustInventory.xaml
    /// </summary>
    public partial class dlgAdjustInventory : Window
    {
        private SalesManagerEntities _context;
        private tblInventory _inventory;
        private int _physicalInventoryCount;

        public dlgAdjustInventory(SalesManagerEntities context, tblInventory inventory)
        {
            _context = context;
            _inventory = inventory;
            InitializeComponent();

            this.DataContext = _inventory.Product;
            //CollectionViewSource productViewSource = (CollectionViewSource)FindResource("Product");
            //productViewSource.Source = _inventory.Product;
            lblAvailableInventory.Content = _inventory.Qty.ToString();
            qryInventoryLevels vwInventory = (from c in _context.qryInventoryLevels
                                              where c.InventoryID == inventory.InventoryID
                                              select c).First();

            // Add in the allocated values to get the physical quantity in stock
            _physicalInventoryCount = _inventory.Qty + (int)vwInventory.Allocated + (int)vwInventory.Printed;
            lblPhysicalInventory.Content = _physicalInventoryCount;
            if (inventory.Product.UnitCost == null)
                txtUnitCost.Text = "0";
            else
                txtUnitCost.Text = ((decimal)inventory.Product.UnitCost).ToString("C");
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            int qty;
            if (!int.TryParse(txtQty.Text, out qty))
            {
                MessageBox.Show("Invalid quantity");
                return;
            }
            switch ((string)cmbAdjustmentType.SelectionBoxItem)
            {
                case "Add To Inventory":
                    break;
                case "Remove From Inventory":
                    qty = 0 - qty;
                    break;
                case "Set Inventory from count":
                    qty = qty - _physicalInventoryCount;
                    break;
                default:
                    return;
            }
            decimal unitCost;
            if (!decimal.TryParse(txtUnitCost.Text, 
                System.Globalization.NumberStyles.AllowCurrencySymbol | System.Globalization.NumberStyles.AllowDecimalPoint, 
                System.Globalization.CultureInfo.CurrentCulture , out unitCost))
            {
                MessageBox.Show("Invalid unit cost");
                return;
            }

            _context.spAdjustInventory(_inventory.InventoryID, qty, unitCost, "A" );

            this.DialogResult = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
